package com.mgr.view.pdf;

import java.util.ArrayList;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFBuilder extends AbstractITextPdfView {

	@Override
	protected void buildPdfDocument(Map<String, Object> model, Document doc,
			PdfWriter writer, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		// get data
		@SuppressWarnings("unchecked")
		ArrayList<String[]> printData = (ArrayList<String[]>) model.get("printData");
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		//String[] title = printData.get(0);
		String title = printData.get(0)[1];
		
		// define image
		String path = classLoader.getResource("resources/image/captionbg30.png").getPath();
		Image image = Image.getInstance(path);
		image.scaleAbsolute(524, 25);
		PdfContentByte over = writer.getDirectContent();
		over.saveState();
        over.beginText();
        over.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
        over.setLineWidth(1.5f);
        over.setRGBColorStroke(0xFF, 0xFF, 0xFF);
        over.setRGBColorFill(0xFF, 0xFF, 0xFF);
        over.setFontAndSize(BaseFont.createFont(), 15);
        over.setTextMatrix(50, 788);
        over.showText(title);
        over.endText();
        over.restoreState();
		
        // define table
		PdfPTable table = new PdfPTable(2);
		table.setWidthPercentage(100.0f);
		table.setWidths(new float[] {40.0f,60.0f});
		
		// define color
        BaseColor boderCellColor = new BaseColor(14, 88, 65);
        BaseColor LLightGrey = new BaseColor(236, 237, 234);
        
		// define font
		Font fontCourierWhite = FontFactory.getFont(FontFactory.COURIER);
		fontCourierWhite.setColor(BaseColor.BLACK);
		
		Font fontCourierDarkGreen = FontFactory.getFont(FontFactory.COURIER_BOLD);
		fontCourierDarkGreen.setColor(boderCellColor);
		
		// define table cell 1
		PdfPCell cell1 = new PdfPCell();
		cell1.setBackgroundColor(BaseColor.LIGHT_GRAY);	
		cell1.setBorderColor(BaseColor.WHITE);
		cell1.setPadding(5);
		
		// define table cell 2
		PdfPCell cell2 = new PdfPCell();	
		cell2.setBackgroundColor(LLightGrey);	
		cell2.setBorderColor(BaseColor.WHITE);
		cell2.setPadding(5);
		cell2.setPaddingLeft(20);
		
		// write table header 
		printData.remove(0);
		for (String[] entry : printData) {
			cell1.setPhrase(new Phrase(entry[0], fontCourierWhite));
			table.addCell(cell1);
			
			cell2.setPhrase(new Phrase(entry[1], fontCourierDarkGreen));
			table.addCell(cell2);
        }
		doc.add(image);
		doc.add(table);
		
	}

}